//
//  OfflineFileViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/6.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "OfflineFileViewModel.h"

#import "OfflineFileCellModel.h"

#import "OfflineFileDBData.h"

@interface OfflineFileViewModel()

@property (nonatomic, readwrite, strong) RACCommand* requestDataCmd;

@property (nonatomic, readwrite, strong) RACCommand* selectCmd;

@end

@implementation OfflineFileViewModel

- (void)initialize
{
    [super initialize];
    
    @weakify(self);
    
    self.cellHeight = 88.0f;
    
    self.lastPage = 100;
    self.shouldMultiSections = NO;
    /// viewDidLoad后加载数据
    self.shouldRequestRemoteDataOnViewDidLoad = YES;
    /// 允许下拉刷新
    self.shouldPullDownToRefresh = YES;
    /// 允许上拉加载
    self.shouldPullUpToLoadMore = YES;
    
    /// 选中
    self.selectCmd = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSIndexPath * indexPath) {
        
        @strongify(self);
        OfflineFileCellModel* cm = self.dataSource[indexPath.row];
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0), dispatch_get_main_queue(), ^{
                [subscriber sendNext:cm.data];
                [subscriber sendCompleted];
            });
            return nil;
        }];
    }];
    /// 允许并行执行
    self.selectCmd.allowsConcurrentExecution = NO;
    self.didSelectCommand = self.selectCmd;
}

- (RACSignal *)requestRemoteDataSignalWithPage:(NSUInteger)page
{
    @weakify(self);
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        @strongify(self);
        
        @weakify(self)
        DataManager* dataMgr = [DataManager sharedDataManager];
        int historyCount = [dataMgr.dbMgr jq_tableItemCount:@"OfflineFileHistory"];
        
        int passNum = (int)page * List_PageSize;
        int actualPageSize = (passNum + List_PageSize) <= historyCount ? List_PageSize : (historyCount - passNum);
        
        NSString* strFormat = HCStrFormat(@"ORDER BY pkid DESC limit %d,%d", (int)page*List_PageSize, actualPageSize);
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSArray* readHistoryDBDatas = [dataMgr.dbMgr jq_lookupTable:@"OfflineFileHistory" dicOrModel:[OfflineFileDBData class] whereFormat:strFormat];
            
            // 转化数据
            @weakify(self);
            NSArray *viewModels = [readHistoryDBDatas.rac_sequence map:^OfflineFileCellModel *(OfflineFileDBData * dbData) {
                @strongify(self);
                
                OfflineFileCellModel *itemViewModel = [[OfflineFileCellModel alloc] initWithData:dbData cellHeight:88.0f];
                
                return itemViewModel;
            }].array;
            
            if (page == 0)
            {
                self.lastPage = 999;
                self.dataSource = viewModels;
            }
            else
            {
                if (viewModels.count == 0)
                {
                    self.lastPage = page;
                    
                    [MBProgressHUD mh_showTips:@"已经没有更多的数据"];
                }
                self.dataSource = [self.dataSource arrayByAddingObjectsFromArray:viewModels];
            }
            
            [subscriber sendNext:nil];
            [subscriber sendCompleted];
        });
        
        return nil;
    }];
}

@end
